package cn.cs.service.impl;

import cn.cs.client.ProductClient;
import cn.cs.client.ProductTypeClient;
import cn.cs.common.*;
import cn.cs.pojo.Product;
import cn.cs.pojo.ProductType;
import cn.cs.service.ProductTypeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProductTypeServiceImpl implements ProductTypeService {

    @Resource
    private ProductTypeClient productTypeClient;
    @Resource
    private ProductClient productClient;
    public ProductType getProductTypeById(String id)throws Exception{
        return productTypeClient.getProductTypeById(id);
    }

    public List<ProductType> getProductTypeListByMap(Map<String,Object> param)throws Exception{
        return productTypeClient.getProductTypeListByMap(param);
    }

    public List<ProductType> getTypeTree(){
        return productTypeClient.getTypeTree();
    }

    public Integer getProductTypeCountByMap(Map<String,Object> param)throws Exception{
        return productTypeClient.getProductTypeCountByMap(param);
    }

    public Integer addProductType(ProductType productType)throws Exception{
        return productTypeClient.addProductType(productType);
    }

    public Integer modifyProductType(ProductType productType)throws Exception{
        return productTypeClient.modifyProductType(productType);
    }

    public Integer deleteProductTypeById(String id)throws Exception{
        return productTypeClient.deleteProductTypeById(id);
    }

    public Page<ProductType> queryProductTypePageByMap(Map<String,Object> param, Integer pageNo, Integer pageSize)throws Exception{
        Integer total = productTypeClient.getProductTypeCountByMap(param);
        pageNo = EmptyUtils.isEmpty(pageNo) ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = EmptyUtils.isEmpty(pageSize) ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<ProductType> productTypeList = productTypeClient.getProductTypeListByMapPage(param);
        page.setRows(productTypeList);
        return page;
    }

    @Override
    public Dto getProType(String token) {
        List<Map<String,Object>> list = productTypeClient.getProTypes();
        if(!EmptyUtils.isEmpty(list)){
            return DtoUtil.returnDataSuccess(list);
        }else{
            return DtoUtil.returnFail("此商品种类不存在","002");
        }
    }

    @Override
    public Dto getProTypeByPid(HttpServletRequest request) {
        String pid = request.getParameter("pid");
        List<Map<String,Object>> list =  productTypeClient.getProTypesByPid(pid);
        if(!EmptyUtils.isEmpty(list)){
            return DtoUtil.returnDataSuccess(list);
        }else{
            return DtoUtil.returnFail("此种类不存在","002");
        }
    }

    @Override
    public Dto getProByType(HttpServletRequest request) {
        String typeId = request.getParameter("typeId");
        Map<String,Object> param = new HashMap<String,Object>();
        param.put("typeId",typeId);
        String pageNum = request.getParameter("pageNum");
        param.put("begNum",(Integer.valueOf(pageNum)-1)*10);param.put("endNum",Integer.valueOf(pageNum)*10);
        String locals = request.getParameter("local");
        List<Product> list = new ArrayList<Product>();
        String sales = request.getParameter("sales");
        if(EmptyUtils.isNotEmpty(locals)) {
            String[] local = locals.split(":");
            param.put("lon", local[0]);
            param.put("lat", local[1]);
        }
        if(EmptyUtils.isNotEmpty(sales)){
            list =  productTypeClient.getproBySales(param);
            if(!EmptyUtils.isEmpty(list)){
                for(Product pro :list){
                    pro.setCount(String.valueOf(productClient.getCellCount(pro.getId())));
                }
            }
        }
        else {
            if(EmptyUtils.isNotEmpty(locals)) {
                list = productTypeClient.getProsBydis(param);
                if (!EmptyUtils.isEmpty(list)) {
                    for (Product pro : list) {
                        pro.setCount(String.valueOf(productClient.getCellCount(pro.getId())));
                    }
                }
            }else{
                list = productTypeClient.getproBySales(param);
                if (!EmptyUtils.isEmpty(list)) {
                    for (Product pro : list) {
                        pro.setCount(String.valueOf(productClient.getCellCount(pro.getId())));
                    }
                }
            }
        }
        if(!EmptyUtils.isEmpty(list)){
            return DtoUtil.returnDataSuccess(list);
        } else{
            return DtoUtil.returnFail("此类商品不存在","002");
        }
    }
}
